import Toast from './index.vue'

let currentToast
export default {
  install(Vue, options) {
    // 通过传参的方式接受回调
    Vue.prototype.$toast = (toastOptions, closeCallback) => {
      currentToast = createToast({
        Vue,
        propsData: toastOptions,
        onClose: () => {
          closeCallback && closeCallback()
          currentToast = null
        }
      })
    }
  }
}

function createToast({ Vue, propsData, onClose }) {
  const Constructor = Vue.extend(Toast)
  let toast = new Constructor({
    // 这是传入组件的props
    propsData
  })
  toast.$mount()
  // 接受close事件
  toast.$on('close', onClose)
  document.body.appendChild(toast.$el)
  return toast
}